<?php

namespace App\Admin\Forms;

use App\Constants\HealthNoteConstant;
use App\Models\HealthNote;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class ExamineNote extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle($input)
    {
        $id = $this->payload['id'];
        $status = $input['status'];

        if (!array_key_exists($status, HealthNoteConstant::EXAMINE_STATUS_MAP)) return $this->response()->error('参数错误');

        $info = HealthNote::find($id);
        if (!$info) return $this->response()->error('信息不存在');

        if ($info->examine_status !== HealthNoteConstant::WAIT_EXAMINE) return $this->response()->error('该笔记已处理');

        $info->update([
            'examine_status' => $status,
            'examine_time'   => time(),
        ]);

        return $this->response()->success('操作成功')->refresh();
    }

    public function form()
    {
        $payload = $this->payload;

        $this->text('title', '标题')->value($payload['title'] ?? '')->disable();
        $this->textarea('content', '内容')->value($payload['content'] ?? '')->disable();
        $this->radio('status', '审核')
            ->options([
                HealthNoteConstant::FAIL_EXAMINE => '驳回',
                HealthNoteConstant::SUC_EXAMINE  => '通过',
            ]);
    }
}
